#include "all.h"
extern size_t get_filesize(int fd);//获得文件大小
extern int is_dir(char* path);//判断是否目录
extern int endwith(char* s,char c);
extern char* str_contact(char* str1,char* str2);//拼接
extern void *thread_copy_fn(void *arg);//
extern void Copy(char *spathname,char *tpathname);
extern void copy_folder(char* source_path,char *destination_path);
extern int remove_dir(const char *dir);//删除
extern void traverse(char* path,char* path2);//拷贝用
extern void traverseAB(char* path,char* path2);//
extern void traverseBA(char* path,char* path2);//删除用
extern void ProgressBar();//进度条
int main()
{
    struct stat buf;
    char path_A[1000],path_B[1000];
    char temp1[1000],temp2[1000];
    DIR *dir_A,*dir_B;

    int choose;
    printf("欢迎使用\n");
    printf("1.把A备份到B\n");
    printf("2.把B同步到A\n");
    printf("3.把A同步到B\n");
    printf("4.A,B同时同步\n");
    printf("5.退出本系统\n");
    printf("请输入序号:\n");
    scanf("%d",&choose);
    if(choose==5){
    	 //退出
	    printf("欢迎下次使用!\n");
		exit(1);
    }
    printf("请输入A的路径:");
    scanf("%s",path_A);
    dir_A=opendir(path_A);
    if(dir_A==NULL)
    {
        printf("This diretory don't exist!\n");
        return 0;
        }
    if(stat(path_A,&buf)!=0)
    {
        printf("ERROR!\n");
        return 0;
        }
    printf("请输入B的路径:");
    scanf("%s",path_B);
    dir_B=opendir(path_B);
    if(dir_B==NULL)
    {
        mkdir(path_B,buf.st_mode);
        chown(path_B,buf.st_uid,buf.st_gid);
        dir_B=opendir(path_B);
        }
    else
    {
        chmod(path_B,buf.st_mode);
        chown(path_B,buf.st_uid,buf.st_gid);
    }

    switch(choose){
        case 1:
        	printf("开始备份..........\n");
		strcpy(temp1,path_A);
		strcpy(temp2,path_B);
		traverseAB(temp1,temp2);
		traverseBA(temp2,temp1);
		ProgressBar();
		printf("备份完成\n");
        break;
        case 2:
        	 //把B同步到A
		printf("开始同步..........\n");
		traverseAB(path_B,path_A);
		traverseBA(path_A,path_B);
		ProgressBar();
		printf("同步完成\n");
        break;
        case 3:
         	 //把A同步到B
		printf("开始同步..........\n");
		traverseAB(path_A,path_B);
		traverseBA(path_B,path_A);
		ProgressBar();
		printf("同步完成\n");
        break;
        case 4:
		 //把A,B同时同步
		printf("开始同步..........\n");
		traverse(path_A,path_B);
		traverse(path_B,path_A);
		ProgressBar();
		printf("同步完成\n");
        break;
        default:printf("输入错误\n"); break;
    }
       return 0;

}
